using UnityEngine;
using UnityEditor;
using System.Collections;
 
public class CharControl : MonoBehaviour {
 	public float walkSpeed;
	
	private Transform myTransform;				// this transform
	private Vector3 destinationPosition;		// The destination Point
	private float destinationDistance;			// The distance between myTransform and destinationPosition	
	private float moveSpeed;						// The Speed the character will move
 	private string[] _anims = {"idle","walk","shot","die1","die2"};
	private GameObject _cursor;
	private int _ncursor=4;
	
	enum _AnimState	{
		idle=0,
		walk,
		shot,
		die
	};
	private _AnimState _state=_AnimState.idle;
	
	void Start () {
		myTransform = transform;							// sets myTransform to this GameObject.transform
		destinationPosition = myTransform.position;			// prevents myTransform reset
		animation["idle"].wrapMode = WrapMode.Loop;
		animation["walk"].wrapMode = WrapMode.Loop;
		animation["shot"].wrapMode = WrapMode.Loop;
		animation["walk"].speed = 1f+(walkSpeed*.05f);
	}
 
	void Update () {
 
		// keep track of the distance between this gameObject and destinationPosition
		destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
 
		if(destinationDistance < .5f){		// To prevent shakin behavior when near destination
			moveSpeed = 0;
			if(_state!=_AnimState.idle)
			{
				_state=_AnimState.idle;
				animation.Play("idle",PlayMode.StopAll);
			}
		}
		else if(destinationDistance > .5f){			// To Reset Speed to default
			moveSpeed = walkSpeed;
			if(_state!=_AnimState.walk)
			{
				_state=_AnimState.walk;
				animation.Play("walk",PlayMode.StopAll);
			}
		}
 
 
		// Moves the Player if the Left Mouse Button was clicked
		if (Input.GetMouseButtonDown(0)&& GUIUtility.hotControl ==0) {
 
			Plane playerPlane = new Plane(Vector3.up, myTransform.position);
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			float hitdist = 0.0f;
 
			if (playerPlane.Raycast(ray, out hitdist)) {
				Vector3 targetPoint = ray.GetPoint(hitdist);
				destinationPosition = ray.GetPoint(hitdist);
				Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
				myTransform.rotation = targetRotation;
			}
			GameObject fab = UnityEngine.Resources.LoadAssetAtPath("Assets/Local/Objects/cursor.fbx",typeof(GameObject)) as GameObject;
			_cursor = (GameObject)Instantiate(fab, destinationPosition, transform.rotation);
			_cursor.transform.localScale= new Vector3 (2,2,2);
    		Destroy(_cursor, 1); // if bullet isn't a GameObject, use bullet.gameObject
		}
 
		// Moves the player if the mouse button is hold down
		else if (Input.GetMouseButton(0)&& GUIUtility.hotControl ==0) {
 
			Plane playerPlane = new Plane(Vector3.up, myTransform.position);
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			float hitdist = 0.0f;
 
			if (playerPlane.Raycast(ray, out hitdist)) {
				Vector3 targetPoint = ray.GetPoint(hitdist);
				destinationPosition = ray.GetPoint(hitdist);
				Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
				myTransform.rotation = targetRotation;
			}
		//	myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
		}
 
		// To prevent code from running if not needed
		if(destinationDistance > .5f){
			myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
		}
	}
}